<template>
	<view>
		<u-navbar title="设置" :autoBack="true" :placeholder="true" leftIconColor="#ffffff"
			:titleStyle="{color: '#ffffff'}" bgColor="#1D1D20" class="navBarHeight" />

		<view class="w-100 p-l-20 p-r-10 border-box bg-white">
			<view class="flex align-center justify-between p-t-15 p-b-15 font-28"
				:class="index == list.length-1 ?  '':'border-bottom' " v-for="(item, index) in list" :key="index"
				@click="toLink(item)">
				<view>{{item.name}}</view>
				<view class="flex align-center text-black9">
					<text class="margin-right-sm" v-if="item.type == 'editMobile'">{{$dealTel(userInfo.mobile)}}</text>
					<text class="margin-right-sm maxWidth" v-if="item.type == 'editName'">{{userInfo.nickname}}</text>
					<text class="margin-right-sm" v-if="item.type == 'editPhoto'">
						<u-avatar :src="userInfo.head" size="80rpx" @click="changeAvatar" />
					</text>
					<text class="margin-right-sm maxWidth"
						v-if="item.type == 'version'">{{versionInfo.versionName}}</text>
					<u-icon name="arrow-right" color="#999999" size="18"
						v-if="item.type !== 'editPhoto' || item.type !== 'version' || item.type !== 'editMobile'" />
				</view>
			</view>
		</view>

		<view class="w-100 p-l-20 p-r-10 border-box bg-white m-t-20">
			<!-- #ifdef APP-PLUS -->
			<view class="flex align-center justify-between p-t-15 p-b-15 font-28" @click="clearStorage">
				<view>清理缓存</view>
				<view class="flex align-center text-black9">
					<text class="margin-right-sm maxWidth">{{fileSizeString}}</text>
				</view>
			</view>
			<!-- #endif -->
			<view class="flex align-center justify-between p-t-15 p-b-15 font-28"
				:class="index == list1.length-1 ?  '':'border-bottom' " v-for="(item, index) in list1" :key="index"
				@click="toLink(item)">
				<view>{{item.name}}</view>
				<view class="flex align-center text-black9">
					<text class="margin-right-sm" v-if="item.type == 'editMobile'">{{$dealTel(userInfo.mobile)}}</text>
					<text class="margin-right-sm maxWidth" v-if="item.type == 'editName'">{{userInfo.nickName}}</text>
					<text class="margin-right-sm" v-if="item.type == 'editPhoto'">
						<u-avatar :src="userInfo.head" size="80rpx" @click="changeAvatar" />
					</text>
					<text class="margin-right-sm maxWidth"
						v-if="item.type == 'version'">{{versionInfo.versionName}}</text>
					<u-icon name="arrow-right" color="#999999" size="18"
						v-if="item.type !== 'editPhoto' || item.type !== 'version' || item.type !== 'zhuxiao'" />
				</view>
			</view>
		</view>
		<view class="m-t-50">
			<view class="padding font-32 text-center w-80 loginOutBox bg-yellow" @click="loginOut">
				<view>退出登录</view>
			</view>
		</view>
		<!--		<u-modal :show="areaShow" title="注销账号" @confirm="confirmAdd" @cancel="cancelAdd" :showCancelButton="true">-->
		<!--			<view class="slot-content">-->
		<!--				<input type="text" v-model="password" placeholder="请输入账户密码" class="inputPwd" />-->
		<!--			</view>-->
		<!--		</u-modal>-->

		<payPassword ref="payPasswordRef" @pay="handlePay"></payPassword>
	</view>
</template>

<script>
	// #ifdef APP-PLUS || H5
	import {
		IMLoginOut
	} from "../../TUIKit/utils/checkLogin";
	// #endif
	import {
		customerInfo,
		loginOff
	} from '@/common/api/customer.js';
	import {
		changeUserinfo
	} from '@/common/api/api.js';
	import {
		lastVersion
	} from '@/common/api/api.js';
	import {
		uploadFile
	} from '@/common/utils/uploadImg.js';
	export default {
		data() {
			return {
				fileSizeString: null,
				userInfo: {},
				versionInfo: {},
				list: [{
						name: '用户头像',
						type: 'editPhoto',
						url: null
					},
					{
						name: '用户昵称',
						type: 'editName',
						url: '/pages/my/setting/editName'
					},
					{
						name: '手机号',
						type: 'editMobile',
					},
					{
						name: '修改登录密码',
						type: 'resetPassword',
						url: '/pages/my/setting/resetPassword'
					},
					// {
					// 	name: '修改支付密码',
					// 	type: 'resetPayPassword',
					// 	url: '/pages/my/setting/resetPayPassword'
					// },
					{
						name: '用户协议',
						type: 'xieyi',
						url: '/pages/my/userAgreement',
					},
					{
						name: '隐私政策',
						type: 'zhengce',
						url: '/pages/my/privacyPolicy',
					},
				],
				list1: [
					// {
					// 	name: '版本号',
					// 	type: 'version',
					// 	url: ''
					// },
					// {
					// 	name: '注销账号',
					// 	type: 'zhuxiao',
					// 	url: '',
					// }
				],
				password: null,
				areaShow: false
			};
		},
		onShow() {
			this.getCustomerInfo()
			this.getLastVersion()

			//获取缓存
			// #ifdef APP-PLUS
			this.formatSize();
			// #endif
		},
		methods: {
			//清理缓存
			clearStorage() {
				uni.showModal({
					title: '清除缓存',
					content: '您确定要清除缓存吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.clearCache();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 获取缓存
			formatSize() {
				plus.cache.calculate((size) => {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						this.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						this.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			// 清理缓存
			clearCache() {
				uni.showLoading({
					title: '清理中'
				});
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i];
						plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
							if (entry.isDirectory) {
								entry.removeRecursively((entry) => {
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									this.formatSize();
								}, (e) => {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, (e) => {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios
					plus.cache.clear(() => {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						this.formatSize();
					});
				}
			},

			handlePay(password) {
				this.confirmAdd(password)
			},

			confirmAdd(password = {}) {
				// if(!this.password){
				// 	this.$msg('请输入账号密码!')
				// 	return
				// }

				if (!password.password) {
					this.$refs.payPasswordRef.show = true
					return
				}


				let params = {
					payCipher: password.password
					// userPassword: this.password
				}
				loginOff(params).then(res => {
					this.$msg(res.msg)
					if (res.code == 200) {
						this.areaShow = false
						// this.$refs.payPasswordRef.close()
						setTimeout(() => {
							this.loginOut()
						}, 1000)
					}
				})
			},
			cancelAdd() {
				this.areaShow = false
			},
			// 退出登录
			loginOut() {
				uni.clearStorageSync()
				uni.setStorageSync('agreementBool',true)
				// IMLoginOut()
				uni.reLaunch({
					url: '/pages/login/login'
				})
			},
			// 获取版本号
			getLastVersion() {
				lastVersion(1).then(res => {
					if (res.code == 200) {
						this.versionInfo = res.data || {}
					}
				})
			},
			// 获取用户信息
			getCustomerInfo() {
				customerInfo().then(res => {
					this.userInfo = res.data.user || {}
					this.$store.commit('user/setUserInfo', res.data || {})
				})
			},
			toLink(item) {
				let that = this
				if (item.type == 'zhuxiao') {
					uni.showModal({
						title: '注销提示',
						content: '注销即用户本人自愿放弃该账号奖励权益，奖励作废且销毁。是否确认注销！(本人人参与的奖励作废停止发放。订单其他有效人的奖励正常按规则发放。)',
						success: function(res) {
							if (res.confirm) {
								// that.areaShow = true
								that.confirmAdd()
							}
						}
					})
					that.password = null
					return
				}
				uni.navigateTo({
					url: item.url
				})
			},
			//修改头像
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					success: res => {
						if (res) {
							let result = uploadFile(res.tempFilePaths[0], 9)
							result.then(ress => {
								if (ress.code == 200) {
									let path = ress.data[0].browseUrl
									this.changeInfoFunc(path)
								}
							})
						}
					}
				})
			},
			changeInfoFunc(path) {
				let params = {
					head: path
				}
				changeUserinfo(params).then(res => {
					if (res.code == 200) {
						this.$msg('修改成功')
						setTimeout(() => {
							this.getCustomerInfo()
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.maxWidth {
		max-width: 350rpx;
	}

	.loginOutBox {
		color: #ffffff;
		border-radius: 20rpx;
		margin: 0 auto;
	}

	.inputPwd {
		display: block;
		padding: 0 20rpx;
		height: 88rpx;
		width: 100%;
		border-radius: 10rpx;
		background-color: #f5f5f5;
		color: #000;
	}
</style>